using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;


namespace TroTro
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class CarHealth
    {
        #region Fields

        //CarHealth objects
        private TroTro _trotro;
        private readonly Game _game;
        private Texture2D _tireTexture;
        private Texture2D _carFrame;
        private Texture2D _texture;
        private Texture2D _gauge;

        private Color _colorRight;

        private Color TireColorRight
        {
            get { return _colorRight; }
            set { _colorRight = value; }
        }

        private Color _colorLeft;

        private Color TireColorLeft
        {
            get { return _colorLeft; }
            set { _colorLeft = value; }
        }

        private Color _colorLeftBack;

        public Color TireColorLeftBack
        {
            get { return _colorLeftBack; }
            set { _colorLeftBack = value; }
        }

        private Color _colorRightBack;

        public Color TireColorRightBack
        {
            get { return _colorRightBack; }
            set { _colorRightBack = value; }
        }

        public CarHealth(Game game, TroTro trotro)
        {
            _game = game;
            _trotro = trotro;
        }

        #endregion

        #region Methods

        public void LoadContent(ContentManager content)
        {
            //_tireTexture = content.Load<Texture2D>(@"sprites\healthbars\TireHealthBar");
            _carFrame = content.Load<Texture2D>(@"sprites\healthbars\carFrame");
            _texture = content.Load<Texture2D>(@"sprites\healthbars\NewTire");
            _gauge = content.Load<Texture2D>(@"sprites\MPHGauge\gauge");
        }

        public void Draw(SpriteBatch sBatch)
        {
            #region Search Components

            foreach (var gc in _game.Components.Where(gc => gc is TroTro))
            {
                _trotro = (TroTro) gc;
            }

            #endregion

            #region Set Health Color

            //Sets the color of the tires depending on the damage
            TireColorRight = _trotro.RightTireHealth < 250 ? Color.Red : Color.Green;
            TireColorLeft = _trotro.LeftTireHealth < 250 ? Color.Red : Color.Green;
            TireColorLeftBack = _trotro.LeftBackTireHealth < 125 ? Color.Red : Color.Green;
            TireColorRightBack = _trotro.RightBackTireHealth < 125 ? Color.Red : Color.Green;

            #endregion

            #region Gauge

            sBatch.Draw(_gauge, new Rectangle(-50, 540, 200, 200), new Rectangle(0, 0, 380, 350), Color.White);

            #endregion

            #region Frame

            //Draws the car frame
            sBatch.Draw(_carFrame, new Rectangle(35, 621, 40, 60), new Rectangle(0, 0, 176, 153), Color.White);

            #endregion

            #region Right Front

            //Draw the background behind the health bar
            sBatch.Draw(_texture, new Rectangle(67, 605, 47, 40), new Rectangle(1, 82, 150, 79), Color.White);

            //Draw the frame around the background
            sBatch.Draw(_texture, new Rectangle(65, 605, 50, 40), new Rectangle(0, 1, 150, 79), Color.White);


            //Draw the current health level based on the current Health
            sBatch.Draw(_texture, new Rectangle(67, 605,(int) ((47 * ((double)_trotro.RightTireHealth / 500))), 40),
                    new Rectangle(1, 82, 150, 79), _colorRight);

            #endregion

            #region Right Rear

            //Draw the background behind the health bar
            sBatch.Draw(_texture, new Rectangle(67, 658, 47, 40), new Rectangle(1, 82, 150, 79), Color.White);

            //Draw the frame around the background
            sBatch.Draw(_texture, new Rectangle(65, 658, 50, 40), new Rectangle(0, 1, 150, 79), Color.White);


            //Draw the current health level based on the current Health
            sBatch.Draw(_texture, new Rectangle(67, 658, (int)((47 * ((double)_trotro.RightBackTireHealth / 250))), 40),
                    new Rectangle(1, 82, 150, 79), _colorRightBack);

            #endregion

            #region Left Front

            //Draw the background behind the health bar
            sBatch.Draw(_texture, new Rectangle(9, 605, 47, 40), new Rectangle(1, 82, 150, 79), Color.White);

            //Draw the frame around the background
            sBatch.Draw(_texture, new Rectangle(7, 605, 50, 40), new Rectangle(0, 1, 150, 79), Color.White);


            //Draw the current health level based on the current Health
            sBatch.Draw(_texture, new Rectangle(9, 605, (int)((47 * ((double)_trotro.LeftTireHealth / 500))), 40),
                    new Rectangle(1, 82, 150, 79), _colorLeft);

            #endregion

            #region Left Rear

            //Draw the background behind the health bar
            sBatch.Draw(_texture, new Rectangle(9, 658, 47, 40), new Rectangle(1, 82, 150, 79), Color.White);

            //Draw the frame around the background
            sBatch.Draw(_texture, new Rectangle(7, 658, 50, 40), new Rectangle(0, 1, 150, 79), Color.White);


            //Draw the current health level based on the current Health
            sBatch.Draw(_texture, new Rectangle(9, 658, (int)((47 * ((double)_trotro.LeftBackTireHealth / 250))), 40),
                    new Rectangle(1, 82, 150, 79), _colorLeftBack);

            #endregion
        }

        #endregion
    }
}
